/*
 PureMVC AS3 Demo - Flex CafeTownsend
 Copyright (c) 2007-08 Michael Ramirez <michael.ramirez@puremvc.org>
 Parts Copyright (c) 2005-07 Adobe Systems, Inc. 
 Your reuse is governed by the Creative Commons Attribution 3.0 License
 */
/*
Copyright 2011 The Apache Software Foundation

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package idv.takeshi.model
{
	import idv.takeshi.*;
	import idv.takeshi.model.vo.Person;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.AbstractOperation;
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.remoting.RemoteObject;
	
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.proxy.Proxy;
    
    /**
     * A proxy for the Employee data
     */
    public class PersonProxy extends Proxy implements IProxy, IResponder
    {
		public static const NAME:String 							= "PersonProxy";
		
		private static const VALIDATE_OPPERATION_NAME:String 		= "validate";
		private static const GET_ALL_PERSON_OPPERATION_NAME:String 	= "getAll";
		private static const UPDATE_PERSON_OPPERATION_NAME:String 	= "update";
		private static const ADD_PERSON_OPPERATION_NAME:String 		= "add";
		private static const DELETE_PERSON_OPPERATION_NAME:String 	= "delete";
		
		private var personService:RemoteObject = null;

		public function PersonProxy (data:Object = null, endpoint:String = null) 
        {
            super ( NAME, data );
			
			personService = new RemoteObject("personService");
			personService.endpoint = endpoint;
        }
		
		public function validate(accountName:String, password:String):void
		{
			var token:AsyncToken = personService.getOperation(VALIDATE_OPPERATION_NAME).send(accountName, password);
			token.addResponder(this);
		}
		
		public function loadPersons():void
		{
			var token:AsyncToken = personService.getOperation(GET_ALL_PERSON_OPPERATION_NAME).send();
			token.addResponder(this);
		}
		
		// this is called when the delegate receives a result from the service
		public function result( rpcEvent : Object ) : void
		{
			var operation:String = rpcEvent.token.message.operation;
			
			switch(operation)
			{
				case VALIDATE_OPPERATION_NAME:
					data = rpcEvent.result;
					sendNotification(ApplicationFacade.APP_LOGIN_SUCCESS);
					break;
				case GET_ALL_PERSON_OPPERATION_NAME:
					data = rpcEvent.result;
					sendNotification(ApplicationFacade.LOAD_PERSONS_SUCCESS);
					break;
				case UPDATE_PERSON_OPPERATION_NAME:
					data = "Update Person info successfully!";
					sendNotification(ApplicationFacade.UPDATE_PERSON_SUCCESS);
					break;
				case DELETE_PERSON_OPPERATION_NAME:
					data = "Delete Person successfully!"
					sendNotification(ApplicationFacade.DELETE_PERSON_SUCCESS);
					break;
				case ADD_PERSON_OPPERATION_NAME:
					data = "Add new Person successfully!"
					sendNotification(ApplicationFacade.ADD_PERSON_SUCCESS);
					break;
				default:
					data = null;
			}
			
			// populate the employee list in the proxy with the results from the service call
//			data = rpcEvent.result.employees.employee as ArrayCollection;
//			sendNotification( ApplicationFacade.LOAD_EMPLOYEES_SUCCESS );
		}
		
		// this is called when the delegate receives a fault from the service
		public function fault( rpcEvent : Object ) : void 
		{
			var operation:String = rpcEvent.token.message.operation;
			switch(operation)
			{
				case VALIDATE_OPPERATION_NAME:
					data = "Validate account name & password failed!";
					sendNotification(ApplicationFacade.APP_LOGIN_FAILED);
					break;
				case GET_ALL_PERSON_OPPERATION_NAME:
					data = "Load persons data failed!";
					sendNotification(ApplicationFacade.LOAD_PERSONS_SUCCESS);
					break;
				case UPDATE_PERSON_OPPERATION_NAME:
					data = "Update Person info failed!";
					sendNotification(ApplicationFacade.UPDATE_PERSON_FAILED);
					break;
				case DELETE_PERSON_OPPERATION_NAME:
					data = "Delete Person failed!"
					sendNotification(ApplicationFacade.DELETE_PERSON_FAILED);
					break;
				case ADD_PERSON_OPPERATION_NAME:
					data = "Add new Person failed!"
					sendNotification(ApplicationFacade.ADD_PERSON_FAILED);
					break;
				default:
			}
		}
		
		public function updatePerson(person:Person):void
		{
			var token:AsyncToken = personService.getOperation(UPDATE_PERSON_OPPERATION_NAME).send(person);
			token.addResponder(this);	
		}
		
		public function addPerson(person:Person):void
		{
			var token:AsyncToken = personService.getOperation(ADD_PERSON_OPPERATION_NAME).send(person);
			token.addResponder(this);
		}
		
		public function deletePerson(personId:Number):void
		{
			var token:AsyncToken = personService.getOperation(DELETE_PERSON_OPPERATION_NAME).send(personId);
			token.addResponder(this);	
		}
		
		public function get employeeListDP():ArrayCollection
		{
			return data as ArrayCollection;
		}
		
	}
}